package com.vv.vv_commonlibrary.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.TextView;

import com.vv.vv_commonlibrary.R;


/**
 * 自定义的ToolBarLayout
 */
public class ToolBarLayout extends ConstraintLayout {

    private TextView toolBarTitle;
    private TextView backTv;
    private TextView rightTv;
    private ImageView rightIv;
    private ImageView moreIv;

    public ToolBarLayout(Context context) {
        super(context);
        initView(null, 0);
    }

    public ToolBarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs, 0);
    }

    public ToolBarLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(attrs, defStyle);
    }

    private void initView(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ToolBarLayout, defStyle, 0);
        int baseLayoutId = a.getResourceId(R.styleable.ToolBarLayout_baseLayoutId, R.layout.toolbar_layout);
        String title = a.getString(R.styleable.ToolBarLayout_toolBarTitle);
        String back = a.getString(R.styleable.ToolBarLayout_toolBarBackText);
        int bgColor = a.getColor(R.styleable.ToolBarLayout_toolBarLayoutColor, ContextCompat.getColor(getContext(), R.color.colorPrimary));
        int titleColor = a.getColor(R.styleable.ToolBarLayout_toolBarTitleColor, ContextCompat.getColor(getContext(), R.color.white));
        int leftTextColor = a.getColor(R.styleable.ToolBarLayout_toolBarLeftTextColor, ContextCompat.getColor(getContext(), R.color.white));
        int rightTextColor = a.getColor(R.styleable.ToolBarLayout_toolBarRightTextColor, ContextCompat.getColor(getContext(), R.color.white));
        setBackgroundColor(bgColor);
        int menuLayoutId = a.getResourceId(R.styleable.ToolBarLayout_menuLayoutId, -1);

        LayoutInflater.from(getContext()).inflate(baseLayoutId, this);
        backTv = findViewById(R.id.back);
        toolBarTitle = findViewById(R.id.toolBarTitle);
        rightIv = findViewById(R.id.right_iv);
        toolBarTitle.setText(title);
        toolBarTitle.setTextColor(titleColor);
        rightTv = findViewById(R.id.right_tv);
        moreIv = findViewById(R.id.more_iv);
        String rightText = a.getString(R.styleable.ToolBarLayout_toolBarRightText);
        Drawable rightdr = a.getDrawable(R.styleable.ToolBarLayout_toolBarRightIcon);
        Drawable rightTextDrawableLeft = a.getDrawable(R.styleable.ToolBarLayout_toolBarRightTvDrawableLeft);
        if (rightTextDrawableLeft != null) {

            rightTextDrawableLeft.setBounds(0, 0, rightTextDrawableLeft.getMinimumWidth(), rightTextDrawableLeft.getMinimumHeight());
            rightTv.setCompoundDrawablePadding(10);
            rightTv.setCompoundDrawables(rightTextDrawableLeft, null, null, null);
        }
        if (!TextUtils.isEmpty(rightText)) {
            rightTv.setVisibility(VISIBLE);
            rightTv.setText(rightText);
            rightTv.setTextColor(rightTextColor);
            if (rightdr != null) {
                rightdr.setBounds(0, 0, rightdr.getMinimumWidth(), rightdr.getMinimumHeight());
                rightTv.setCompoundDrawables(null, rightdr, null, null);
                rightTv.setTextSize(12);
            }
        } else {
            if (rightdr != null) {
                rightIv.setVisibility(VISIBLE);
                rightIv.setImageDrawable(rightdr);
            }
        }
        Drawable rightIvRes = a.getDrawable(R.styleable.ToolBarLayout_toolBarRightIvBg);
        rightIv.setBackground(rightIvRes);
        if (menuLayoutId != -1) {
            LayoutInflater.from(getContext()).inflate(menuLayoutId, this);
        }
        backTv.setTextColor(leftTextColor);
        if (!TextUtils.isEmpty(back)) {
            backTv.setText(back);
        }
        boolean disableBackIv = a.getBoolean(R.styleable.ToolBarLayout_disableBackIv, false);
        Drawable drawable = a.getDrawable(R.styleable.ToolBarLayout_toolBarLeftIcon);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            backTv.setCompoundDrawables(drawable, null, null, null);
        } else {
            drawable = getResources().getDrawable(R.drawable.toolbar_back);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            backTv.setCompoundDrawables(drawable, null, null, null);
        }
        if (disableBackIv) {
            backTv.setVisibility(GONE);
        } else {
            backTv.setVisibility(VISIBLE);
        }
        a.recycle();
    }

    public void setLeftIcon(Drawable drawable) {
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            backTv.setCompoundDrawables(drawable, null, null, null);
        }
    }

    public void setLeftText(String txt) {
        backTv.setText(txt);
    }

    public void setTitleTxt(String titleTxt) {
        toolBarTitle.setText(titleTxt);
    }

    public TextView getTitleView() {
        return toolBarTitle;
    }

    public void setTitleColor(int titleColor) {
        toolBarTitle.setTextColor(titleColor);
    }

    public void setBackColor(int color) {
        backTv.setTextColor(color);
    }

    public TextView getRightTv() {
        return rightTv;
    }

    public void setRightTvSize(int size) {
        rightTv.setTextSize(size);
    }

    public ImageView getRightIv() {
        return rightIv;
    }

    public ImageView getMoreIv() {
        return moreIv;
    }

    public TextView getBackTv() {
        return backTv;
    }

}
